<?php

namespace app\services;

use plugin\admin\app\model\PageContent;
use plugin\admin\app\model\PageContentPosition;

class PageContentService
{
    /**
    * Get static content list by position ID.
    *
    * @param int $positionId
    * @return array
    */
    public function getStaticContentListByPositionId($positionIds): array
    {
        if (!is_array($positionIds)) {
            $positionIds = [$positionIds];
        }

        $data = [];
        foreach ($positionIds as $positionId) {
            // Fetch the PageContentPosition record
            $position = PageContentPosition::where('id', $positionId)->first();
            if (!$position) {
                return []; // Return empty array if position not found
            }

            // Decode hidden_fields into an array
            $hiddenFields = explode(',', $position->hidden_fields);

            // Fetch content based on position ID, sorted by 'sort' field
            $contents = PageContent::where('position_id', $positionId)
                ->orderBy('sort', 'asc')
                ->get()
                ->toArray();

            // Process the 'img' field with the full_url function
            foreach ($contents as &$content) {
                $content['img'] = full_url($content['img']);
                $content['img1'] = full_url($content['img1']);
                $content['active_img'] = full_url($content['active_img']);
                $content['icon'] = full_url($content['icon']);
                $content['active_icon'] = full_url($content['active_icon']);
                $content['video'] = full_url($content['video']);

                $content['raw_desc'] =  explode("\n", $content['desc']);

                if ($content['id'] != 6 && $content['id'] != 4) {
                    $content['title'] = get_current_language() == 'en' ? $content['en_title'] : $content['title'];
                    $content['desc'] = get_current_language() == 'en' ? $content['en_desc'] : $content['desc'];
                    $content['content'] = get_current_language() == 'en' ? $content['en_content'] : $content['content'];
                } else {

                    if ($content['id'] == 4) {
                        //
                    } else {
                        $content['desc'] = get_current_language() == 'en' ? $content['en_desc'] : $content['desc'];
                    }
                }

                // Process 'desc' field
                if (isset($content['desc']) && strpos($content['desc'], "\n") !== false) {
                    $descArray = explode("\n", $content['desc']);
                    $content['desc'] = implode('', array_map(fn($line) => "<p>{$line}</p>", $descArray));
                }

                // Remove hidden fields
                foreach ($hiddenFields as $hiddenField) {
                    unset($content[$hiddenField]);
                }
            }

            $data[$positionId] = [
                'position_id' => $positionId,
                'name' => $position->name,
                'contents' => $contents,
            ];

        }

        return $data;
    }
}